package com.iss.card.webservice;

import java.util.concurrent.atomic.AtomicInteger;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.iss.card.R;
import com.iss.card.common.CardProgressBar;
import com.iss.card.common.CommonValue;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.common.Tools;
import com.iss.card.webservice.WebServiceAsyncServiceHelper.OperationInfo;

public class WebServiceAsyncService extends Handler {
	private final static String TAG = WebServiceAsyncService.class
			.getSimpleName();
	// Used for generating unique tokens for calls to this service
	private static AtomicInteger mUniqueToken = new AtomicInteger(0);

	private Handler mHandler = this;

	private PostExecute mPostExecute;

	private Context mContext;

	private CardProgressBar mProgressBar;

	/**
	 * returns a practically unique token for db operations
	 */
	public final int getNextToken() {
		return mUniqueToken.getAndIncrement();
	}

	public static class Operation {

		public static final int OP_SERVICE_ARG_USER_REGISTRATION = 1;
		public static final int OP_SERVICE_ARG_SHOPS_INFO = 2;
		public static final int OP_SERVICE_ARG_CARD_INFO = 3;
		public static final int OP_SERVICE_ARG_PRODUCT_INFO = 4;
		public static final int OP_SERVICE_ARG_VIP_ORDER = 5;
		public static final int OP_SERVICE_ARG_CARD_HISTORY_TRACE = 6;
		public static final int OP_SERVICE_ARG_STORE_CARD_ADDRESS = 7;

		public static final int OP_SERVICE_ARG_ACCOUNT_REGISTER = 8;
		public static final int OP_SERVICE_ARG_ACCOUNT_LOGIN = 9;
		public static final int OP_SERVICE_ARG_SEND_MESSAGE = 10;
		public static final int OP_SERVICE_ARG_CHANGE_PASSWORD = 11;
		public static final int OP_SERVICE_ARG_SHAKE_RESUTIL = 12;
		public static final int OP_SERVICE_ARG_FEED_BACK = 13;
		public static final int OP_SERVICE_ARG_NEAR_STORE = 14;

		public static final int OP_SERVICE_ARG_COLLECTION = 15;
		public static final int OP_SERVICE_ARG_GET_COLLECTION = 16;
		public static final int OP_SERVICE_ARG_GET_CITYLIST = 17;
		public static final int OP_SERVICE_ARG_GET_CAPTCHA = 18; // add by qiubo
																	// at
																	// 2013-03-21
		public static final int OP_SERVICE_ARG_GET_STORE_INFO_BY_ID = 19;
		public static final int OP_SERVICE_ARG_GET_STORE_LIST_BY_TYPE = 20;
		public static final int OP_SERVICE_ARG_GET_STORE_TYPE_BY_CATEGORY = 21;
		public static final int OP_SERVICE_ARG_GET_NO_PAY_LIST = 22;
		public static final int OP_SERVICE_ARG_PAY_PENDING_ORDER = 23;
		public static final int OP_SERVICE_ARG_RETURN_ORDER = 24;
		public static final int OP_SERVICE_ARG_GET_CARD_LIST_BY_STORE_ID = 25;

		protected static char opToChar(int op) {
			switch (op) {
			case Operation.OP_SERVICE_ARG_USER_REGISTRATION:
				return 'A';
			case Operation.OP_SERVICE_ARG_SHOPS_INFO:
				return 'B';
			case Operation.OP_SERVICE_ARG_CARD_INFO:
				return 'C';
			case Operation.OP_SERVICE_ARG_PRODUCT_INFO:
				return 'D';
			case Operation.OP_SERVICE_ARG_VIP_ORDER:
				return 'E';
			case Operation.OP_SERVICE_ARG_CARD_HISTORY_TRACE:
				return 'F';
			case Operation.OP_SERVICE_ARG_STORE_CARD_ADDRESS:
				return 'G';
			case Operation.OP_SERVICE_ARG_ACCOUNT_REGISTER:
				return 'H';
			case Operation.OP_SERVICE_ARG_ACCOUNT_LOGIN:
				return 'I';
			case Operation.OP_SERVICE_ARG_SEND_MESSAGE:
				return 'J';
			case Operation.OP_SERVICE_ARG_CHANGE_PASSWORD:
				return 'K';
			case Operation.OP_SERVICE_ARG_SHAKE_RESUTIL:
				return 'L';
			case Operation.OP_SERVICE_ARG_FEED_BACK:
				return 'M';
			case Operation.OP_SERVICE_ARG_NEAR_STORE:
				return 'N';
			case Operation.OP_SERVICE_ARG_COLLECTION:
				return 'O';
			case Operation.OP_SERVICE_ARG_GET_COLLECTION:
				return 'P';
			case Operation.OP_SERVICE_ARG_GET_CITYLIST:
				return 'Q';
			case Operation.OP_SERVICE_ARG_GET_CAPTCHA: // add by qiubo at
														// 2013-03-21
				return 'R';

			case Operation.OP_SERVICE_ARG_GET_STORE_INFO_BY_ID:
				return 'S';
			case Operation.OP_SERVICE_ARG_GET_STORE_LIST_BY_TYPE:
				return 'T';
			case Operation.OP_SERVICE_ARG_GET_STORE_TYPE_BY_CATEGORY:
				return 'U';
			case Operation.OP_SERVICE_ARG_GET_NO_PAY_LIST:
				return 'V';
			case Operation.OP_SERVICE_ARG_PAY_PENDING_ORDER:
				return 'W';
			case Operation.OP_SERVICE_ARG_RETURN_ORDER:
				return 'X';
			case Operation.OP_SERVICE_ARG_GET_CARD_LIST_BY_STORE_ID:
				return 'Y';
			default:
				return '?';
			}
		}
	}

	/**
	 * 
	 * @param context
	 *            context
	 * @param token
	 *            current token
	 * @param op
	 *            operate flag
	 * @param parameterName
	 *            key
	 * @param parameterValues
	 *            value
	 * @param servicePortName
	 *            the name of the url
	 * @param ibkl
	 *            listener for dialog
	 * @param index
	 *            http request type
	 */
	public void setParameterToAsyncService(Context context, int token, int op,
			String[] parameterName, String[] parameterValues,
			String servicePortName, IBackKeyListener ibkl, int index) {
		if (context == null) {
			return;
		}
		this.mContext = context;
		if (!Tools.isNetworkConnected(context)) {
			Tools.showToast(context, R.string.no_network_presentation);
			if (ibkl != null) {
				ibkl.cancelUpdate(CommonValue.ErrorType.TYPE_NETWORK);
			}
			return;
		}
		WebServiceAsyncServiceHelper wash = new WebServiceAsyncServiceHelper();
		// mProgressBar = new CardProgressBar(context,wash,ibkl);
		// mProgressBar.show();

		OperationInfo info = new OperationInfo();
		info.parameterName = parameterName;
		info.parameterValues = parameterValues;
		info.servicePortName = servicePortName;
		info.handler = mHandler;
		info.op = op;
		info.token = token;
		info.index = index;
		wash.queueOperation(wash, info);
	}

	@Override
	public void handleMessage(Message msg) {
		super.handleMessage(msg);
		// mProgressBar.cancel();
		OperationInfo info = (OperationInfo) msg.obj;

		int token = msg.what;

		switch (msg.arg1) {
		case Operation.OP_SERVICE_ARG_USER_REGISTRATION:
		case Operation.OP_SERVICE_ARG_SHOPS_INFO:
		case Operation.OP_SERVICE_ARG_CARD_INFO:
		case Operation.OP_SERVICE_ARG_PRODUCT_INFO:
		case Operation.OP_SERVICE_ARG_VIP_ORDER:
		case Operation.OP_SERVICE_ARG_CARD_HISTORY_TRACE:
		case Operation.OP_SERVICE_ARG_STORE_CARD_ADDRESS:
		case Operation.OP_SERVICE_ARG_ACCOUNT_REGISTER:
		case Operation.OP_SERVICE_ARG_ACCOUNT_LOGIN:
		case Operation.OP_SERVICE_ARG_SEND_MESSAGE:
		case Operation.OP_SERVICE_ARG_CHANGE_PASSWORD:
		case Operation.OP_SERVICE_ARG_SHAKE_RESUTIL:
		case Operation.OP_SERVICE_ARG_FEED_BACK:
		case Operation.OP_SERVICE_ARG_NEAR_STORE:
		case Operation.OP_SERVICE_ARG_COLLECTION:
		case Operation.OP_SERVICE_ARG_GET_COLLECTION:
		case Operation.OP_SERVICE_ARG_GET_CITYLIST:
		case Operation.OP_SERVICE_ARG_GET_CAPTCHA: // add by qiubo at 2013-03-21
		case Operation.OP_SERVICE_ARG_GET_STORE_INFO_BY_ID:
		case Operation.OP_SERVICE_ARG_GET_STORE_LIST_BY_TYPE:
		case Operation.OP_SERVICE_ARG_GET_STORE_TYPE_BY_CATEGORY:
		case Operation.OP_SERVICE_ARG_GET_NO_PAY_LIST:
		case Operation.OP_SERVICE_ARG_PAY_PENDING_ORDER:
		case Operation.OP_SERVICE_ARG_RETURN_ORDER:
		case Operation.OP_SERVICE_ARG_GET_CARD_LIST_BY_STORE_ID:
			if (mPostExecute != null) {
				mPostExecute.onAsyncServiceComplete(token, info.result,
						info.index);
			}
			break;

		default:
			break;
		}
	}

	public void setPostExecute(PostExecute p) {
		this.mPostExecute = p;
	}

	public static interface PostExecute {

		void onAsyncServiceComplete(int token, Object result, int index);
	}

}
